/*
This file is part of Cafca.

Cafca is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cafca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Cafca.  If not, see <http://www.gnu.org/licenses/>.

If you have any comment, suggestion or idea, do not hesitate to contact me at shenrotin@gmail.com
*/

package org.crag.cafca.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.crag.cafca.gui.Submenu;
import org.crag.cafca.gui.Menu;
import org.crag.cafca.util.Constants;

public class XmlConnector 
{
	/**
	 * Logger log4j. See log4j doc for more information.
	 */
	private static Logger log=Logger.getLogger(new Throwable().getStackTrace()[0].getClassName());
		
	private String xmlFileName;
	private Document dom;
	private static DocumentBuilder builder;
	
	static
	{
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		try 
		{
			builder=factory.newDocumentBuilder();
		} 
		catch (ParserConfigurationException e) 
		{
			e.printStackTrace();
		}
	}
	
	public XmlConnector(String xmlFileName) throws FileNotFoundException, SAXException, IOException
	{
		this.xmlFileName=xmlFileName;
		InputStream is=new FileInputStream(xmlFileName);
		log.info("Parsing file : "+xmlFileName);
		dom=builder.parse(is);
		is.close();
	}
	
	public void close() 
	{
		//Nothing to do
	}

	public Menu loadMenu() 
	{
		Menu result=new Menu();
		Element rootElement=dom.getDocumentElement();
		
		NodeList subsystemNodeList=rootElement.getChildNodes();
		for (int i=0;i<subsystemNodeList.getLength();i++)
		{
			Node node=subsystemNodeList.item(i);
			if (!(node instanceof Element))
				continue;
			Element subsystemElement=(Element)node;
			Submenu subsystem=this.loadSubsystem(subsystemElement);
			result.addSubmenu(subsystem);
		}
		
		return result;
	}	

	private Submenu loadSubsystem(Element subsystemElement) 
	{
		Submenu subsystem=new Submenu();
		subsystem.setId(subsystemElement.getAttribute(Constants.XML_ID));
		subsystem.setName(subsystemElement.getAttribute(Constants.XML_NAME));
		
		String url=subsystemElement.getAttribute(Constants.XML_URL);
		
		if ((url!=null)&&(url.length()!=0))
			subsystem.setUrl(url);
		
		NodeList subsystemNodeList=subsystemElement.getChildNodes();
		for (int i=0;i<subsystemNodeList.getLength();i++)
		{
			Node node=subsystemNodeList.item(i);
			if (!(node instanceof Element))
				continue;
			Element childElement=(Element)node;
			Submenu childSubSystem=this.loadSubsystem(childElement);
			subsystem.addSubmenu(childSubSystem);
		}
		
		return subsystem;
	}

}
